package controllers;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import utils.UsersInfo;

import models.Customer;
import models.PostQuestion;
import models.ReplyQuestion;
import models.SolutionSubmission;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyService;
import com.googlecode.objectify.Query;

public class ajxShowQuestionResponseServlet extends HttpServlet {
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		resp.setContentType("text/xml");
		PrintWriter out = resp.getWriter();
        UserService userService = UserServiceFactory.getUserService();
        String userEmailID = userService.getCurrentUser().getEmail(); 
		
		String SolID = req.getParameter("sol_id");
		if ( SolID == null ){
			resp.sendRedirect("/home.jsp?error=Solution id is null.");
		}
		Long lSolID = Long.parseLong(SolID);

		String  htmlstr = "", tempstr = "";
		int counter = 1;
		Objectify ofy = ObjectifyService.begin();
		Query<PostQuestion> q = ofy.query(PostQuestion.class).filter("solID", lSolID);
		for ( PostQuestion f : q.fetch() ) {
			htmlstr	=	htmlstr + "<div id='Qaccordion" + counter + "' >";
			htmlstr	=	htmlstr	+ "<h3><a href='#'>Question by " + UsersInfo.getFullName(f.getQuestionBy()) + " on " + f.getQuestionON() + "</a></h3>";
			htmlstr	=	htmlstr	+ "<div style='table-layout:fixed; width:100%; overflow:hidden; word-wrap:break-word;'>";
			htmlstr	=	htmlstr	+ f.getQuestion() + "<div id='r" + f.getQuestID() + "' title='Reply Question' ></div>";
			htmlstr	=	htmlstr	+ "<br><span style='position:relative;left:440px' >";
			tempstr	=	"<a align='right' href='/needtofigureout'><img align='right' src='/img/reply_icon.png'><b>Reply</b></a>";
			htmlstr	=	htmlstr + "<a align='right' href='#'  class='cancel' id='" + f.getQuestID() + "' ><img align='right' src='/img/reply_icon.png'><b>Reply</b></a></span></div>";
			

			
			Query<ReplyQuestion> qq = ofy.query(ReplyQuestion.class).filter("solID", lSolID).filter("questID", f.getQuestID());
			for ( ReplyQuestion ff : qq.fetch() ) {
				htmlstr	=	htmlstr	+ "<h3><a href='#'>Response by " + UsersInfo.getFullName(ff.getReplyBy()) + " on " + ff.getReplyON() + "</a></h3>";
				htmlstr	=	htmlstr	+	"<div style='table-layout:fixed; width:100%; overflow:hidden; word-wrap:break-word;'>";
				htmlstr	=	htmlstr + ff.getReply() + "<div id='r" + ff.getQuestID() + " title='Reply'></div>";				
				htmlstr	=	htmlstr + "<br><span style='position:relative;left:440px' ><a href='#' class='cancel' id='" + ff.getQuestID() + "' ><img align='right' src='/img/reply_icon.png'><b>Reply</b></a></span></div>";
			}
			
			htmlstr	=	htmlstr+ "</div>";			
			counter++;
		}
		htmlstr	=	htmlstr + "<div id='counterQ' style='display:none'>" + (counter-1) + "</div>";
		out.println(htmlstr);
		
		//htmlstr=	getResponses(qdoc,htmlstr,tempstr,usr)
		//counter=counter+1
		//Set qdoc=qcoll.GetNextDocument(qdoc)	
	}
}
